package sicau.carManager.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author ITcz
 * @Data 2021-02-10 - 15:14
 * adminController只需要验证是否登录，supAdminController还需要对权限进行验证
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    LoginInterceptor loginInterceptor;

    @Autowired
    CorsInterceptor corsInterceptor;

    @Autowired
    SuperAdminIntercepter superAdminIntercepter;

    @Autowired
    AdminArgumentResolver adminArgumentResolver;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //解决跨域
        registry.addInterceptor(corsInterceptor).addPathPatterns("/**");
        //登录验证
        registry.addInterceptor(loginInterceptor).addPathPatterns("/admin/**").addPathPatterns("/black-list/**").addPathPatterns("/fixed-car-information/**").addPathPatterns("/supAdmin/**").addPathPatterns("/violation-information/**")
                .excludePathPatterns("/admin/loginIn").excludePathPatterns("/violation-information/select/violationInformation/by/licensePlate");
        //超级管理员身份认证
        registry.addInterceptor(superAdminIntercepter).addPathPatterns("/black-list/**").addPathPatterns("/fixed-car-information/**").addPathPatterns("/supAdmin/**").addPathPatterns("/violation-information/**").addPathPatterns("/notice/**")
                .excludePathPatterns("/notice/showNotice").excludePathPatterns("/violation-information/select/violationInformation/by/licensePlate");
    }

    /**
     *  springMVC的controller层方法里面可以有很多参数，request,response,model等
     *  框架回调resolveArgument方法给controller里面参数赋值
     *  ——所有只需要遍历这个方法里面的参数看是否有我们需要的Admin对象，有就添加至controller方法里面
    * */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(adminArgumentResolver);
    }

}
